from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional, List


class TagInNote(BaseModel):
    """笔记中的标签"""
    id: int
    name: str
    color: str
    
    class Config:
        from_attributes = True


class NoteBase(BaseModel):
    """笔记基础模型"""
    title: str = Field(..., min_length=1, max_length=200)
    content: str = Field(default="")
    notebook_id: Optional[int] = None
    is_pinned: bool = False


class NoteCreate(NoteBase):
    """笔记创建模型"""
    tag_ids: List[int] = []


class NoteUpdate(BaseModel):
    """笔记更新模型"""
    title: Optional[str] = Field(None, min_length=1, max_length=200)
    content: Optional[str] = None
    notebook_id: Optional[int] = None
    is_pinned: Optional[bool] = None
    tag_ids: Optional[List[int]] = None


class NoteResponse(NoteBase):
    """笔记响应模型"""
    id: int
    user_id: int
    tags: List[TagInNote] = []
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True


class NoteListResponse(BaseModel):
    """笔记列表响应"""
    total: int
    notes: List[NoteResponse]


class NoteHistoryResponse(BaseModel):
    """笔记历史响应"""
    id: int
    note_id: int
    content: str
    version: int
    created_at: datetime
    
    class Config:
        from_attributes = True

